package co.fitstart.coach.web.controller.courseware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.coach.service.CoursewareService;
import co.fitstart.entity.courseware.fitnessgroup.BatchCourseware;
import co.fitstart.entity.courseware.fitnessgroup.Courseware;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareMovementType;
import co.fitstart.entity.courseware.fitnessgroup.DailyCourseware;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/rounds/{roundId}/batch-coursewares/{batchId}/daily-coursewares/{dailyCoursewareId}")
public class DailyCoursewareController {
    
    private static final Logger logger = LoggerFactory.getLogger(DailyCoursewareController.class);
    
    @Autowired
    private CoursewareService coursewareService;
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @return
     */
    @RequestMapping(value = "/rest", method = RequestMethod.PATCH)
    public @ResponseBody String onRest(@PathVariable("roundId") Long roundId, 
                                       @PathVariable("batchId") Long batchId,
                                       @PathVariable("dailyCoursewareId") Long dailyCoursewareId){
        
        Result result = new Result();
        
        try{
            
            DailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(dailyCoursewareId);
            
            coursewareService.rest(dailyCourseware);
            
            result.success().message("设为休息日成功！");
        }
        catch (Exception e) {
            
            logger.error("onRest - ", e);
            result.fail().message("设为休息日失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @param type
     * @return
     */
    @RequestMapping(value = "/coursewares/create", method = RequestMethod.GET)
    public ModelAndView create(@PathVariable("roundId") Long roundId, 
                               @PathVariable("batchId") Long batchId,
                               @PathVariable("dailyCoursewareId")int dailyCoursewareId,
                               @RequestParam(value = "type", required = false) String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/daily/courseware-create");
        
        modelAndView.addObject("roundId", roundId);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("dailyCoursewareId", dailyCoursewareId);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @param type
     * @param courseware
     * @return
     */
    @RequestMapping(value = "/coursewares", method = RequestMethod.POST)
    public @ResponseBody String onCreate(@PathVariable("roundId") Long roundId, 
                                         @PathVariable("batchId") Long batchId,
                                         @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                         @RequestParam(name = "movementType", required = false) String type,
                                         Courseware courseware){
        
        Result result = new Result();
        
        try{
            
            DailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(dailyCoursewareId);
            
            coursewareService.createCourseware(dailyCourseware, courseware, CoursewareMovementType.aliasOf(type));
            
            result.success().message("添加动作成功！");
        } catch (Exception e) {
            
            logger.error("onCreate - ", e);
            result.fail().message("添加动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    @RequestMapping(value = "/coursewares/action-group", method = RequestMethod.POST)
    public @ResponseBody String onActionGroup(@PathVariable("roundId") Long roundId, 
                                              @PathVariable("batchId") Long batchId,
                                              @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                              @RequestParam(name = "movementType", required = false) String type,
                                              @RequestParam("groupId") Long groupId){
        
        Result result = new Result();
        
        try{
            
            DailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(dailyCoursewareId);
            FitnessGroup fitnessGroup = coursewareService.loadFitnessGroup(groupId);
            
            coursewareService.createCoursewareGroup(dailyCourseware, fitnessGroup, CoursewareMovementType.aliasOf(type));
            
            result.success().message("添加动作组成功！");
        } catch (Exception e) {
            
            logger.error("onActionGroup - ", e);
            result.fail().message("添加动作组失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareId
     * @return
     */
    @RequestMapping(value = "/coursewares/{coursewareId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("roundId") Long roundId,
                             @PathVariable("batchId") Long batchId,
                             @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                             @PathVariable("coursewareId") Long coursewareId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/batch/movement-edit");
        
        Courseware courseware = coursewareService.loadCourseware(coursewareId);
        modelAndView.addObject("courseware", courseware);
        
        BatchCourseware batchCourseware = coursewareService.loadBatchCourseware(batchId);
        modelAndView.addObject("batchCourseware", batchCourseware);
        
        modelAndView.addObject("roundId", roundId);
        modelAndView.addObject("dailyCoursewareId", dailyCoursewareId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @param courseware
     * @return
     */
    @RequestMapping(value = "/coursewares/{coursewareId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@PathVariable("roundId") Long roundId,
                                       @PathVariable("batchId") Long batchId,
                                       @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                       @Preloading("coursewareId") Courseware courseware){
        
        Result result = new Result();
        
        try{
            
            coursewareService.editCourseware(courseware);
            
            result.success().message("修改动作成功！");
        } catch (Exception e) {
            
            logger.error("onEdit - ", e);
            result.fail().message("修改动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param roundId
     * @param batchId
     * @param dailyCoursewareId
     * @param courseware
     * @return
     */
    @RequestMapping(value = "/coursewares/{coursewareId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("roundId") Long roundId,
                                         @PathVariable("batchId") Long batchId,
                                         @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                         @Preloading("coursewareId") Courseware courseware){
        
        Result result = new Result();
        
        try{
            
            coursewareService.deleteCourseware(courseware);
            
            result.success().message("删除动作成功！");
        }
        catch (Exception e) {
            
            logger.error("onDelete - ", e);
            result.fail().message("删除动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
	
}
